﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;

namespace Little.Soldier.Licensing
{
    internal class LicensingManager
    {
        private static string _LicenseParameters = "<LicenseParameters><RSAKeyValue><Modulus>nQ0pysT4g60f60z75j6ywGsx+jufRuCPlqXyjnbloOUa2J1MwoW2Cy4rqcxh777ZOWqFJvX1SUdFcPd58iVbPP3W+Fmoma5QJ5lIYmLVv1CCMfqD1i3x2qxu3JcoIRsgc+kAJBKxdYefa/X+OP0A2GDyPYy/b3D+SPIXFBvuZDs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue><DesignSignature>U7Fe7RGfyyRgskOwpXXcpeIT0mpMRGSKLI8AxKVvvsDOdFsQHyFXS81kcZwIAwnOcXr0rnH0dUnjBsXugUyR7bK1kr5Lq4vlHxQ44/lYo1T+m2fVr22JXnRp9eya7Dlmxe4P8IqcekIqrqg0+dNyOCAGynNB5oFzAkfR23GyXzg=</DesignSignature><RuntimeSignature>iMUkeEIVo0+r80aJPe+23dD1QmF+Ia2BnnlV3cNPflBK/8lC1rPEkx3G+T7+VaBc0fTEgM0iDvDEbrS3qXXFH91ood67zPHr3Cp5glL3WeQ/DNre9N0mSADOI/OxjK5UZueEey4IDlW/ExCm1UfXzYVuiV2fU4OBTJ4S2dD1v0s=</RuntimeSignature></LicenseParameters>";
        private static bool _EvalEnd = false;
        private static bool _EvalEndDate = false;
        private static int _EvalDays = 0;
        private static License _IsEval;
    
        public static bool EvalEnd
        {
            get
            {
                return _EvalEnd;
            }
        }

        public static bool IsEval
        {
            get
            {
            return _IsEval == null;
            }
        }

        public static string LicenseKey
        {
            get
            {
                if (_IsEval == null)
                    return (string) null;
            else
                return _IsEval.LicenseKey;
            }
        }

        public static DateTime EvalEndDate
        {
            get
            {
                return new EncryptionService("544DB284-88FE-4F8A-89F4-2B46B7880911").FirstUseDate.AddDays(30.0);
            }
        }

        public static int EvalDays
        {
            get
            {
                return _EvalDays;
            }
        }

        static LicensingManager()
        {
        }

        public static void Validate(System.Type typeOf, object instance)
        {
            License license1 = _IsEval;
            bool flag1 = _EvalEndDate;
            bool flag2 = _EvalEnd;
            string licenseParameters = _LicenseParameters;
            string productID = "544DB284-88FE-4F8A-89F4-2B46B7880911";
            string text1 = "Little Soldier .NET Evaluation has expired. Features are disabled.\nIf you have already licensed the product, please make sure you use Runtime Key to license the product properly.";
            string text2 = "Little Soldier .NET Evaluation has expired, design features will be disabled.\nIf you have already licensed the product, please make sure you use Runtime Key to license the product properly.";
            string format = "Little Soldier .NET Evaluation {0} days left.";
            string productName = "Little Soldier .NET";

            if (license1 == null && !flag1)
            {
                EncryptionProvider.InitLicenseParameters(licenseParameters);
                if (!LicenseManager.IsValid(typeOf, instance, out license1))
                {
                    #region Valid
                    if (LicenseManager.CurrentContext.UsageMode == LicenseUsageMode.Runtime)
                    {
                        #region LicenseUsageMode.Runtime
                        if (license1 == null)
                        {
                            EncryptionService obj1 = new EncryptionService(productID);
                            DateTime date = DateTime.Now.Date;
                            if (date < obj1.LastUseDate.Date)
                            date = obj1.LastUseDate.Date;
                            _EvalDays = 14 - (int) new TimeSpan(date.Ticks - obj1.FirstUseDate.Date.Ticks).TotalDays;
                            if (_EvalDays < 0)
                            {
                            int num = (int) MessageBox.Show(text1, "Evaluation period", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            flag2 = true;
                            }
                            else
                            {
                                RuntimeGetFreeKeyForm obj2 = new RuntimeGetFreeKeyForm();
                                obj2.label1.Text = obj2.label1.Text + "\r\n" + _EvalDays.ToString() + " days left.";
                            int num = (int) obj2.ShowDialog();
                            }
                        }
                        #endregion
                    }            
                    else
                    {
                        #region LicenseInstallForm
                        License license2 = (License)new LicenseInstallForm().GetLicenseInfo(productName, typeOf);
                        Application.DoEvents();
                        if (license2 == null)
                        {
                            EncryptionService obj = new EncryptionService(productID);
                            DateTime date = DateTime.Now.Date;
                            if (date < obj.LastUseDate.Date)
                            date = obj.LastUseDate.Date;
                            _EvalDays = 14 - (int) new TimeSpan(date.Ticks - obj.FirstUseDate.Date.Ticks).TotalDays;
                            if (_EvalDays >= 0)
                            {
                            int num1 = (int) MessageBox.Show(string.Format(format, (object) _EvalDays), "Evaluation period", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            }
                            else
                            {
                            int num2 = (int) MessageBox.Show(text2, "Evaluation period", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            flag2 = true;
                            }
                        }
                        #endregion
                    }
                    _IsEval = (License)null;
                    _EvalEndDate = true;
                    _EvalEnd = flag2;
                    #endregion
                }
                else
                {
                    #region Invalid
                    LicenseInfo obj1 = license1 as LicenseInfo;
                    if (obj1 != null && obj1.FreeLicense)
                    {
                        EncryptionService obj2 = new EncryptionService(productID);
                    DateTime date = DateTime.Now.Date;
                    if (date < obj2.LastUseDate.Date)
                        date = obj2.LastUseDate.Date;
                    _EvalDays = 30 - (int) new TimeSpan(date.Ticks - obj2.FirstUseDate.Date.Ticks).TotalDays;
                    if (_EvalDays < 0)
                    {
                        int num = (int) MessageBox.Show(text1, "Evaluation period", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        LicenseInstallForm obj3 = new LicenseInstallForm();
                        obj3.label1.Visible = false;
                        License license2 = (License)obj3.GetLicenseInfo(productName, typeOf);
                        if (license2 == null)
                        {
                            _IsEval = (License)null;
                            _EvalEndDate = true;
                            _EvalEnd = true;
                        }
                        else
                        {
                            _IsEval = license2;
                            _EvalEndDate = true;
                            _EvalEnd = false;
                        }
                    }
                    else
                    {
                        _IsEval = license1;
                        _EvalEndDate = true;
                        _EvalEnd = false;
                    }
                    }
                    else
                    {
                        _IsEval = license1;
                        _EvalEndDate = true;
                        _EvalEnd = false;
                    }
                    #endregion
                }        
            }

            if (EncryptionProvider.VerifyAssembly(
                new byte[8] {
                    (byte) 111,
                    (byte) 166,
                    (byte) 132,
                    (byte) 62,
                    (byte) 196,
                    (byte) 214,
                    (byte) 189,
                    (byte) 29
                }, Assembly.GetExecutingAssembly().GetName().GetPublicKeyToken())
            )
                return;

            _IsEval = (License)null;
            _EvalEnd = true;
        }

        public static string ReValidate()
        {
            if (_IsEval == null)
                return string.Empty;
            else
                return new EncryptionProvider().Encode(_IsEval.LicenseKey);
        }

        internal static void InitLicenseParameters()
        {
            EncryptionProvider.InitLicenseParameters(_LicenseParameters);
        }
    }
}
